package com.ruoyi.web.controller.fenxi;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class CacheOrgData {
    private static final Logger log = LoggerFactory.getLogger(CacheOrgData.class);

    @Resource
    private ThreadPoolTaskExecutor executor;
    @Autowired
    private IRDataService rDataService;
    @Autowired
    private IROrgService rOrgService;
    @Autowired
    private IROrgRiskHistoryService rOrgRiskHistoryService;
    @Autowired
    private IRModelService rModelService;
    @Autowired
    private IROrgRiskEventService rOrgRiskEventService;

    @Autowired
    private IROrgRiskService rOrgRiskService;

    @Autowired
    private IRDataDetailService rDataDetailService;

    @PostConstruct
    public  void  init(){
        executor.execute(()->{
            //地区
//            removeCache(CacheUtils.ORG_KEY);
            List<ROrg> rOrgs = rOrgService.selectROrgList(new ROrg());
//            log.info("地区加载rOrgs"+rOrgs);
//            CacheUtils.put(CacheUtils.ORG_KEY,rOrgs);
            if(!rOrgs.isEmpty()){
                for (ROrg rOrg : rOrgs) {
                    //风险事件
                    removeCache(CacheUtils.EVENT_KEY+rOrg.getId());
                    PageHelper.startPage(1,20);
                    ROrgRiskEvent rOrgRiskEvent = new ROrgRiskEvent();
                    rOrgRiskEvent.setOrgId(rOrg.getId());
                    List<ROrgRiskEvent> rOrgRiskEvents = rOrgRiskEventService.selectROrgRiskEventList(rOrgRiskEvent);
//                    log.info("加载异常事件rOrgRiskEvents"+rOrgRiskEvents);
                    CacheUtils.put(CacheUtils.EVENT_KEY+rOrg.getId(),rOrgRiskEvents);
                    //饼图
                    removeCache(CacheUtils.PIE_KEY);
                    ROrgRisk rOrgRisk = new ROrgRisk();
                    rOrgRisk.setOrgId(rOrg.getId());
                    rOrgRisk.setLev(0);
                    List<ROrgRisk> rOrgRisks = rOrgRiskService.selectROrgRiskList(rOrgRisk);
                    CacheUtils.put(CacheUtils.PIE_KEY,rOrgRisks);



                }
            }
            //历史
            removeCache(CacheUtils.HISTORY_KEY);
            List<ROrgRiskHistory> rOrgRiskHistories = rOrgRiskHistoryService.selectROrgRiskHistoryList(new ROrgRiskHistory());
//            log.info("历史加载rOrgRiskHistories"+rOrgRiskHistories);
            CacheUtils.put(CacheUtils.HISTORY_KEY,rOrgRiskHistories);
            //模型
            removeCache(CacheUtils.MODEL_KEY);
            List<RModel> rModels = rModelService.selectRModelList(new RModel());
//            log.info("模型加载rModels"+rModels);
            CacheUtils.put(CacheUtils.MODEL_KEY,rModels);
            //样本
            RData RData = new RData();
            RData.setOrgId(1L);
            removeCache(RData.getId()+CacheUtils.RDATA_KEY);
            List<RData> rData = rDataService.selectRDataList(RData);
            List<RData> list1 = new ArrayList<>();
            for (RData data : rData) {
                RDataDetail rDataDetail = new RDataDetail ();
                rDataDetail.setParentId(data.getId());
                PageHelper.startPage(1,20);
                List<RDataDetail> rDataDetails = rDataDetailService.selectRDataDetailList(rDataDetail);
                if(!rDataDetails.isEmpty()){
                    for(int i=0;i<20;i++){
                        if(i==rDataDetails.size()){
                            break;
                        }
                        RDataDetail dataDetail = rDataDetails.get(i);
                        RData rData1 = new RData();
                        BeanUtils.copyProperties(data,rData1);
                        rData1.setUrl(dataDetail.getName());
                        list1.add(rData1);
                    }
                }
            }
//            log.info("样本加载rData"+rData);
            CacheUtils.put(1+CacheUtils.RDATA_KEY,list1);
        });

    }
    public void  removeCache(String key){
        if(Objects.nonNull(CacheUtils.get(key))){
            CacheUtils.remove(key);
        }
    }
}
